<template>
  <div class="announcement-manager">
    <!-- 内容卡片 -->
    <div class="content-card">
      <a-table
        :columns="columns"
        :data-source="dataList"
        :pagination="pagination"
        @change="doTableChange"
        :loading="loading"
        class="announcement-table"
        :scroll="{ x: 800 }"
      >
        <template #bodyCell="{ column, record }">
          <!-- 标题列 -->
          <template v-if="column.dataIndex === 'title'">
            <div class="announcement-title">
              <div class="title-text">{{ record.title }}</div>
              <div v-if="isNewAnnouncement(record)" class="new-badge">NEW</div>
            </div>
          </template>

          <!-- 内容列 -->
          <template v-if="column.dataIndex === 'content'">
            <div class="announcement-content">
              {{ record.content }}
            </div>
          </template>

          <!-- 时间列 -->
          <template v-if="column.dataIndex === 'createTime'">
            <div class="time-cell">
              <div class="time-value">{{ formatTime(record.createTime) }}</div>
              <div class="time-label">发布时间</div>
            </div>
          </template>
        </template>

        <!-- 空状态 -->
        <template #emptyText>
          <div class="empty-state">
            <div class="empty-icon">
              <FileTextOutlined />
            </div>
            <div class="empty-text">暂无系统公告</div>
            <div class="empty-description">当前没有发布任何系统公告</div>
          </div>
        </template>
      </a-table>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { listAnnouncementVoByPageUsingPost } from '@/api/announcementController'
import { FileTextOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { computed, onMounted, reactive, ref } from 'vue'

// 列定义
const columns = [
  {
    title: '公告标题',
    dataIndex: 'title',
    key: 'title',
    width: 200,
    ellipsis: true,
  },
  {
    title: '公告内容',
    dataIndex: 'content',
    key: 'content',
    ellipsis: true,
  },
  {
    title: '发布时间',
    dataIndex: 'createTime',
    key: 'createTime',
    width: 160,
    align: 'center' as const,
  },
]

// 数据列表
const dataList = ref<any[]>([])
const total = ref(0)
const loading = ref(false)

// 搜索参数
const searchParams = reactive({
  current: 1,
  pageSize: 10,
  title: '',
})

// 分页参数
const pagination = computed(() => ({
  current: searchParams.current,
  pageSize: searchParams.pageSize,
  total: total.value,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total: number) => `共 ${total} 条公告`,
  pageSizeOptions: ['10', '20', '50'],
}))

// 格式化时间
const formatTime = (time: string) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm')
}

// 判断是否为最新公告（24小时内）
const isNewAnnouncement = (record: any) => {
  const createTime = dayjs(record.createTime)
  const now = dayjs()
  return now.diff(createTime, 'hour') < 24
}

// 获取公告列表
const getAnnouncementList = async () => {
  loading.value = true
  try {
    const res = await listAnnouncementVoByPageUsingPost({
      ...searchParams,
    })

    if (res.data.code === 0 && res.data.data) {
      dataList.value = res.data.data.records || []
      total.value = res.data.data.total || 0
    } else {
      message.error(res.data.message || '获取公告列表失败')
    }
  } catch (error) {
    message.error('获取公告列表失败')
  } finally {
    loading.value = false
  }
}

// 切换表格页数
const doTableChange = (page: any) => {
  searchParams.current = page.current
  searchParams.pageSize = page.pageSize
  getAnnouncementList()
}

onMounted(() => {
  getAnnouncementList()
})
</script>

<style lang="scss" scoped>
.announcement-manager {
  padding: 0;
  background: #f5f7fa;
  min-height: 100vh;
}

// 页面标题区域
.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 30px 24px;
  margin-bottom: 24px;

  .header-content {
    margin-bottom: 24px;

    .page-title {
      font-size: 28px;
      font-weight: 700;
      margin: 0 0 8px 0;
      display: flex;
      align-items: center;

      .title-icon {
        margin-right: 12px;
        font-size: 24px;
      }
    }

    .page-subtitle {
      font-size: 16px;
      opacity: 0.9;
      margin: 0;
    }
  }

  .stats-overview {
    .stat-item {
      background: rgba(255, 255, 255, 0.15);
      backdrop-filter: blur(10px);
      border-radius: 12px;
      padding: 20px;
      display: inline-block;
      min-width: 150px;

      .stat-number {
        font-size: 32px;
        font-weight: 700;
        line-height: 1;
        margin-bottom: 8px;
      }

      .stat-label {
        font-size: 14px;
        opacity: 0.9;
      }
    }
  }
}

// 内容卡片
.content-card {
  margin: 0 24px 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.announcement-table {
  border: none;

  :deep(.ant-table) {
    border-radius: 12px;
  }

  :deep(.ant-table-thead > tr > th) {
    background: #f8fafc;
    font-weight: 600;
    color: #374151;
    border-bottom: 1px solid #e5e7eb;
    padding: 16px;
    font-size: 14px;
  }

  :deep(.ant-table-tbody > tr > td) {
    padding: 16px;
    border-bottom: 1px solid #f0f0f0;
    color: #595959;
    transition: all 0.3s;
  }

  :deep(.ant-table-tbody > tr:hover > td) {
    background: #f8fafc;
  }

  :deep(.ant-table-pagination) {
    margin: 20px;
  }
}

// 表格内容样式
.announcement-title {
  display: flex;
  align-items: center;
  gap: 8px;

  .title-text {
    font-weight: 500;
    color: #1f2937;
    flex: 1;
  }

  .new-badge {
    background: linear-gradient(135deg, #ff6b6b, #ee5a52);
    color: white;
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 10px;
    font-weight: 600;
  }
}

.announcement-content {
  color: #6b7280;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.time-cell {
  .time-value {
    font-weight: 500;
    color: #1f2937;
  }

  .time-label {
    font-size: 12px;
    color: #6b7280;
    margin-top: 4px;
  }
}

// 空状态样式
.empty-state {
  padding: 60px 0;
  text-align: center;

  .empty-icon {
    font-size: 64px;
    color: #d1d5db;
    margin-bottom: 16px;
  }

  .empty-text {
    font-size: 16px;
    color: #6b7280;
    margin-bottom: 8px;
  }

  .empty-description {
    font-size: 14px;
    color: #9ca3af;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .page-header {
    padding: 20px 16px;

    .stats-overview {
      .stat-item {
        min-width: auto;
        width: 100%;
        text-align: center;
      }
    }
  }

  .content-card {
    margin: 0 16px 16px;
  }

  .announcement-table {
    :deep(.ant-table-thead > tr > th),
    :deep(.ant-table-tbody > tr > td) {
      padding: 12px;
    }
  }
}

@media (max-width: 480px) {
  .announcement-title {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .page-title {
    font-size: 24px;
  }
}
</style>
